<template>
  <div class="article-container">
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-form :inline="true" label-width="80px">
        <el-form-item label="关键字">
          <el-input
            v-model="userInfo.keyword"
            placeholder="根据文章标题搜索"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="userInfo.state" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="remove">清除</el-button>
          <el-button type="primary" @click="search">搜索</el-button>
        </el-form-item>
        <el-form-item style="float: right">
          <el-button
            icon="el-icon-edit"
            type="success"
            @click="addDialog = true"
            >新增技巧</el-button
          >
        </el-form-item>
      </el-form>
      <!-- <el-button icon="el-icon-search" type="success">新增技巧</el-button> -->
      <!-- 总计提示框 -->
      <el-alert type="info" show-icon>
        <span slot="title">数据一共{{ total }}条</span>
      </el-alert>
      <!-- 题组列表区域 -->
      <el-table style="margin-top: 20px" :data="articleList" stripe>
        <el-table-column
          label="序号"
          type="index"
          width="50px"
        ></el-table-column>
        <el-table-column label="文章标题" width="400">
          <template slot-scope="slot">
            <span>{{ slot.row.title }}</span>
            <el-popover placement="right" width="800" trigger="click">
              <video controls :src="slot.row.videoURL" class="video"></video>
              <el-button
                v-if="slot.row.videoURL"
                type="text"
                slot="reference"
                icon="el-icon-film"
              ></el-button>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="visits" label="阅读数"></el-table-column>
        <el-table-column prop="username" label="录入人"></el-table-column>
        <el-table-column label="录入时间" width="200">
          <template slot-scope="slot">
            <span>{{ slot.row.createTime | parseTimeByString }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="slot">
            <span v-if="slot.row.state === 1">已启用</span>
            <span v-else>已禁用</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template slot-scope="slot">
            <el-button type="text" @click="openArticle(slot.row)"
              >预览</el-button
            >
            <el-button type="text" @click="changestate(slot.row)"
              >禁用</el-button
            >
            <el-button
              type="text"
              :disabled="slot.row.state === 1 ? true : false"
              @click="changeArticle(slot.row)"
              >修改</el-button
            >
            <el-button
              type="text"
              :disabled="slot.row.state === 1 ? true : false"
              @click="idDelete(slot.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        style="float: right; margin-top: 20px"
        background
        :page-sizes="[10, 20, 30, 50]"
        layout=" prev, pager, next, sizes, jumper"
        :total="total"
        :current-page="userInfo.page"
        :page-size="userInfo.pagesize"
        @size-change="SizeChange"
        @current-change="currentChange"
      ></el-pagination>
    </el-card>
    <!-- 新增技巧 -->
    <el-dialog
      @close="change = {}"
      title="新增技巧"
      :visible.sync="addDialog"
      width="60%"
    >
      <article-add
        v-if="addDialog"
        :change="change"
        @changeClose="changeClose"
        @close="add"
      ></article-add>
    </el-dialog>

    <!-- 预览文章 -->
    <el-dialog title="预览文章" :visible.sync="articleDialog" width="60%">
      <article-item
        v-if="articleDialog"
        :articleItem="articleItem"
      ></article-item>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="articleDialog = false"
          >关闭</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, remove, changeState } from "@/api/hmmm/articles.js";
import { parseTimeByString } from "../../filters/index";
import articleAdd from "../../module-hmmm/components/articles-add";
import articleItem from "../../module-hmmm/components/articles-preview";
export default {
  name: "Articles-Page",
  components: {
    "article-add": articleAdd,
    "article-item": articleItem,
  },
  data() {
    return {
      // 请求列表的参数
      userInfo: {
        page: 1,
        pagesize: 10,
        keyword: "",
        state: null,
      },
      // 数据总数
      total: 0,
      // 数据列表
      articleList: [],
      // 选择器数据
      options: [
        { value: 1, label: "已启用" },
        { value: 0, label: "已禁用" },
      ],
      // 控制是否弹出预览框
      articleDialog: false,
      // 文章信息
      articleItem: {},
      // 控制新增文章是否弹出
      addDialog: false,
      change: {},
    };
  },
  created() {
    // 一开始就获取文章列表
    this.getArticles();
  },
  methods: {
    // 获取文章列表
    async getArticles() {
      try {
        const { data: data } = await list(this.userInfo);
        console.log(data);
        this.total = data.counts;
        this.articleList = data.items;
      } catch (error) {
        this.$message("获取列表失败");
      }
    },
    // 改变文章数
    SizeChange(changesize) {
      this.userInfo.pagesize = changesize;
      this.getArticles();
    },
    // 改变页码
    currentChange(page) {
      this.userInfo.page = page;
      this.getArticles();
    },
    // 清除
    remove() {
      this.userInfo.keyword = "";
      this.userInfo.state = null;
      this.getArticles();
    },
    // 搜索
    async search() {
      if (this.value == "启用") {
        const { data: data } = await list({
          page: 1,
          pagesize: 10,
          keyword: this.input,
          state: 1,
        });
        this.articleList = data.items;
      } else if (this.value == "禁用") {
        const { data: data } = await list({
          page: 1,
          pagesize: 10,
          keyword: this.input,
          state: 0,
        });
        this.articleList = data.items;
      } else {
        const { data: data } = await list({
          page: 1,
          pagesize: 10,
          keyword: this.input,
        });
        this.articleList = data.items;
      }
      this.getArticles();
    },
    // 更改 状态
    changestate(value) {
      let ArticlesStatus = null;
      if (value.state === 1) {
        ArticlesStatus = 0;
      } else {
        ArticlesStatus = 1;
      }
      try {
        changeState({
          id: value.id,
          state: ArticlesStatus,
        });
        this.getArticles();
        this.$message.success("更改状态成功");
      } catch (error) {
        this.$message("更改状态失败");
      }
    },
    // 删除
    idDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const data = await remove({ id: id });
          if (data.status === 200) {
            this.getArticles();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          }
        })
        .catch();
    },
    // 新增技巧
    add(value) {
      this.addDialog = false;
      this.change = {};
      if (value) {
        this.getArticles();
        this.$message.success("新增文章成功");
      }
    },
    // 预览文章
    openArticle(value) {
      this.articleDialog = true;
      this.articleItem = { ...value };
    },
    // 修改文章
    changeArticle(value) {
      this.addDialog = true;
      // eventbus传数据
      this.change = value;
    },
    changeClose(value) {
      this.addDialog = false;
      this.changeId = 0;
      if (value) {
        this.change = {};
        this.getArticles();
        this.$message.success("修改文章成功");
        console.log(this.change);
      }
    },
  },
};
</script>

<style>
.video {
  width: 100%;
  height: 100%;

}
</style>
